import React from 'react';
import Search from 'components/public/search';
import { Button, Popconfirm, message } from 'antd';
import { connect } from 'dva';
import moment from 'moment';
import router from 'umi/router';

class Construction extends React.PureComponent {
	constructor() {
		super();
		this.state = {};
	}
	componentDidMount() {
		this.getList();
	}
	getList = () => {
		const { dispatch } = this.props;
		dispatch({ type: 'data/getListConstruction' });
	};
	post = () => {
		router.push('/data/construction/post');
	};
	delete = ids => {
		const { dispatch } = this.props;
		dispatch({
			type: 'data/deleteConstruction',
			payload: { ids },
			callback: () => {
				message.success('删除成功!');
				this.getList();
			},
		});
	};
	edit = id => {
		router.push(`/data/construction/${id}/edit`);
	};
	view = id => {
		router.push(`/data/construction/${id}/view`);
	};
	componentWillUnmount() {
		this.props.dispatch({
			type: 'data/save',
			payload: {
				constructionSearch: { startDate: '', endDate: '', pageNum: 1, pageSize: 10 },
			},
		});
	}
	render() {
		const {
			list: { pageData, pageNum, pageSize, total },
		} = this.props;
		const columns = [
			{ dataIndex: 'id', title: '编号', aligh: 'center', width: 80, align: 'center' },
			{
				dataIndex: 'dataTime',
				title: '民生年月',
				width: 100,
				// render: text => moment(text).format('YYYY年MM月'),
			},
			{
				dataIndex: 'physicalExamNum',
				title: '老年人免费体检（人）',
				aligh: 'center',
				width: 150,
			},
			{ dataIndex: 'skillsTrainNum', title: '技能培训（人）', aligh: 'center', width: 150 },
			{ dataIndex: 'moveInNum', title: '迁入人数', aligh: 'center', width: 100 },
			{ dataIndex: 'moveOutNum', title: '迁出人数', width: 100 },
			{ dataIndex: 'birthNum', title: '出生人数', width: 100 },
			{ dataIndex: 'deathNum', title: '死亡人数', width: 100 },
			{ dataIndex: 'infirmNum', title: '五保户', width: 100 },
			{ dataIndex: 'lowIncomeNum', title: '低保户', width: 100 },
			{ dataIndex: 'caressesObjectNum', title: '优抚对象（人）', width: 150 },
			{ dataIndex: 'olderNum', title: '高龄人数', width: 100 },
			{ dataIndex: 'disabilityNum', title: '残疾人总数', width: 150 },
			{
				dataIndex: 'handle',
				title: '操作',
				aligh: 'center',
				width: 200,
				render: (text, record) => (
					<>
						<Button style={{ marginRight: 10 }} onClick={() => this.view(record.id)}>
							查看
						</Button>
						<Button
							type="primary"
							style={{ marginRight: 10 }}
							onClick={() => this.edit(record.id)}
						>
							编辑
						</Button>
						<Popconfirm
							title="确认删除?"
							cancelText="取消"
							okText="删除"
							onConfirm={() => this.delete([record.id])}
						>
							<Button type="danger">删除</Button>
						</Popconfirm>
					</>
				),
			},
		];
		const props = {
			columns,
			dataSource: pageData,
			pageNum,
			pageSize,
			total,
			x: 2500,
			delete: this.delete,
			format: 'YYYY-MM',
			getList: this.getList,
			keyword: false,
			plate: false,
		};
		return (
			<Search {...props}>
				<Button type="primary" onClick={this.post}>
					上传
				</Button>
			</Search>
		);
	}
}
export default connect(state => ({
	list: state.data.constructionList,
}))(Construction);
